<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加商品</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 50%;
            margin: 20px auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        .form-group {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }
        label {
            flex: 1;
            margin-right: 10px;
        }
        input, textarea, button {
            flex: 2;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }
        textarea {
            resize: none;
        }
        button {
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #45a049;
        }
        .cascader {
            position: relative;
            display: inline-block;
            width: 67%;
        }
        .cascader input {
            width: 100%;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
            cursor: pointer;
        }
        .cascader-options {
            position: absolute;
            top: 100%;
            left: 0;
            z-index: 1000;
            display: none;
            width: 100%;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .cascader-options div {
            display: flex;
        }
        .cascader-options select {
            flex: 1;
            padding: 10px;
            border: none;
            box-shadow: none;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>添加商品</h1>
    <form id="addProductForm" enctype="multipart/form-data">
        <div class="form-group">
            <label for="productName">商品名称:</label>
            <input type="text" id="productName" required>
        </div>
        <div class="form-group">
            <label for="productCategory">所属商品类型:</label>
            <div class="cascader" id="productCategory">
                <input type="text" placeholder="请选择" readonly>
                <div class="cascader-options">
                    <div id="categoryLevel0Container">
                        <select id="categoryLevel0"></select>
                    </div>
                    <div id="categoryLevel1Container" style="display:none;">
                        <select id="categoryLevel1"></select>
                    </div>
                    <div id="categoryLevel2Container" style="display:none;">
                        <select id="categoryLevel2"></select>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label for="price">价格:</label>
            <input type="number" id="price" min="0" step="0.01" required>
        </div>
        <div class="form-group">
            <label for="stock">库存:</label>
            <input type="number" id="stock" min="0" required>
        </div>
        <div class="form-group">
            <label for="images">上传图片:</label>
            <input type="file" id="images" accept="image/*" multiple>
        </div>
        <div class="form-group">
            <label for="description">商品描述:</label>
            <textarea id="description" rows="4" required></textarea>
        </div>
        <button type="submit">添加</button>
    </form>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        function populateSelect(select, items) {
            select.empty();
            select.append(new Option("请选择", ""));
            items.forEach(item => {
                select.append(new Option(item.name, item.id));
            });
        }

        const level0Select = $('#categoryLevel0');
        const level1Select = $('#categoryLevel1');
        const level2Select = $('#categoryLevel2');
        const level1Container = $('#categoryLevel1Container');
        const level2Container = $('#categoryLevel2Container');
        const cascaderInput = $('.cascader input');

        // 获取所有商品类型
        $.ajax({
            url: "/product-category",
            method: "GET",
            success: function(data) {
                populateSelect(level0Select, data.filter(item => item.level === 0));

                $('.cascader input').on('click', function(e) {
                    e.stopPropagation();
                    $('.cascader-options').toggle();
                });

                $(document).on('click', function() {
                    $('.cascader-options').hide();
                });

                $('.cascader-options').on('click', function(e) {
                    e.stopPropagation();
                });

                level0Select.on('change', function() {
                    const selectedLevel0 = $(this).val();
                    const level1Items = data.filter(item => item.parentId == selectedLevel0 && item.level === 1);
                    if (level1Items.length > 0) {
                        populateSelect(level1Select, level1Items);
                        level1Container.show();
                        level2Container.hide();
                        cascaderInput.val(level0Select.find("option:selected").text());
                    } else {
                        level1Container.hide();
                        level2Container.hide();
                    }
                });

                level1Select.on('change', function() {
                    const selectedLevel1 = $(this).val();
                    const level2Items = data.filter(item => item.parentId == selectedLevel1 && item.level === 2);
                    if (level2Items.length > 0) {
                        populateSelect(level2Select, level2Items);
                        level2Container.show();
                        cascaderInput.val(level0Select.find("option:selected").text() + ' / ' + level1Select.find("option:selected").text());
                    } else {
                        level2Container.hide();
                    }
                });

                level2Select.on('change', function() {
                    cascaderInput.val(level0Select.find("option:selected").text() + ' / ' + level1Select.find("option:selected").text() + ' / ' + level2Select.find("option:selected").text());
                });
            },
            error: function(error) {
                alert('获取商品类型失败: ' + error.responseText);
            }
        });

        // 添加商品表单提交
        $('#addProductForm').on('submit', function(e) {
            e.preventDefault();

            const product = {
                name: $('#productName').val(),
                productId: level0Select.val(),
                partsId: level2Select.val(),
                price: $('#price').val(),
                stock: $('#stock').val(),
                detail: $('#description').val()
            };

            const formData = new FormData();
            formData.append('product', new Blob([JSON.stringify(product)], { type: 'application/json' }));
            const images = $('#images')[0].files;
            for (let i = 0; i < images.length; i++) {
                formData.append('images', images[i]);
            }

            $.ajax({
                url: "/products",
                method: "POST",
                processData: false,
                contentType: false,
                data: formData,
                success: function(data) {
                    alert('商品添加成功！');
                    $('#addProductForm')[0].reset();
                    level0Select.val('').change(); // 重置商品类型选择
                    cascaderInput.val('请选择');
                },
                error: function(error) {
                    alert('添加商品失败: ' + error.responseText);
                }
            });
        });
    });
</script>
</body>
</html>
